package com.jzqf.wheel;

import android.graphics.Point;
import android.graphics.PointF;

import java.text.DecimalFormat;
import java.util.Random;

public class MathUtil {
    private final static String TAG = "MathUtil";

    /**
     * Returns the shortest angle difference when the inputs range between -180 and 180 (such as from Math.atan2)
     */
    public static float shortestAngle(float angleA, float angleB) {
        float angle = angleA - angleB;
        if (angle > 180f) {
            angle -= 360f;
        } else if (angle < -180f) {
            angle += 360f;
        }
        return angle;
    }


    public static float angleToDegrees(int centerX, int centerY, float x, float y) {
        return (float) Math.toDegrees(angleTo(centerX, centerY, x, y));
    }

    /**
     * The Angle from this circle's center to the position x, y
     * y is considered to go down (like android view system)
     */
    static float angleTo(int centerX, int centerY, float x, float y) {
        return (float) Math.atan2((centerY - y), (x - centerX));
    }

    /**
     * 已知四边形的四个点，求四边形的中心店坐标
     * A与D的连线和B与C的连线交叉,中心点即连线的交叉点
     * y=kx+b
     * ----------
     * <p>
     * ***---------
     *
     * @param pointA 左上
     * @param pointB 右上
     * @param pointC 左下
     * @param pointD 右下
     * @return
     */
    public static Point getCenterPoint(Point pointA, Point pointB, Point pointC, Point pointD) {
//        Log.i(TAG, "pointA x=" + pointA.x + ",y=" + pointA.y + "-->pointD x" + pointD.x + ",y=" + pointD.y);
        Point pointCenter = new Point();
        //认为X差小于此数值则认为是接近垂直线
        double xMin = 0.1;
        //A与D接近同一X坐标时,与Y轴平行
        if (Math.abs(pointA.x - pointD.x) < xMin) {
            pointCenter.x = (pointA.x + pointD.x) / 2;
            pointCenter.y = (pointA.y + pointD.y) / 2;
        } else if (Math.abs(pointB.x - pointC.x) < xMin) {
            pointCenter.x = (pointB.x + pointD.x) / 2;
            pointCenter.y = (pointB.y + pointD.y) / 2;
        } else {
            //求A点与D点的斜率
            double kad = divisionDecimal2((pointA.y - pointD.y), (pointA.x - pointD.x));
            double bad = pointA.y - kad * pointA.x;
            double kbc = divisionDecimal2((pointB.y - pointC.y), (pointB.x - pointC.x));
            double bbc = pointB.y - kbc * pointB.x;
//            Log.d(TAG, "kad=" + kad + ",bad=" + bad + "-->kbc=" + kbc + ",bbc=" + bbc);
            pointCenter.x = (int) divisionDecimal2((bbc - bad), Math.max(kad - kbc, xMin));
            pointCenter.y = (pointA.y + pointC.y) / 2;
//            Log.i(TAG, "xad=" + (pointA.x + pointD.x) + ",xbc=" + (pointB.x + pointC.x));
        }
        return pointCenter;
    }

    /**
     * 除法运算保留2位小数
     *
     * @param a 除数
     * @param b 被除数
     * @return
     */
    public static double divisionDecimal2(double a, double b) {
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        return Double.parseDouble(decimalFormat.format(a / b));
    }

    /**
     * 获取移动的角度
     * Math.hypot(3,4)=5
     */
    public static float getAngle(float xTouch, float yTouch, int centerX, int centerY) {
        double width = xTouch - centerX;
        double height = yTouch - centerY;
        float angle = (float) Math.toDegrees(Math.asin(height / Math.hypot(width, height)));
//        float degree = (float) Math.toDegrees(Math.atan2(height, width));
//        Log.i("getAngle", "angle=" + angle + ",degree=" + degree);
        return angle;
    }


    /**
     * 已知起始点坐标,圆心坐标，半径，弧度，求圆弧结束点在圆上的坐标
     * 仅适用于从坐标系Y轴(第一象限)顺时针方向绘制的算法
     *
     * @param centerX    圆心X坐标
     * @param centerY    圆心Y坐标
     * @param radius     圆半径
     * @param sweepAngle 圆弧度 从起始点到结束点的角度
     * @return 交叉点坐标
     */
    public static PointF getPointEndOfArcOnCircle(PointF pointFStart, float centerX, float centerY, float radius, double sweepAngle) {
        PointF point = new PointF();
        //起始点与Y轴的夹角
        float startHeight = centerY - pointFStart.y;
        float startWidth = pointFStart.x - centerX;
        double angleToY = Math.toDegrees(Math.atan2(startWidth, startHeight));
//        Log.i("CanvasView", "angleToY=" + angleToY);
        sweepAngle = sweepAngle + angleToY;
        //不同坐标系根据偏移量算圆弧中心点坐标
        //tan0°＝0,tan90°不存在,tan180°＝0,tan270°不存在,tan360°＝0
        if (sweepAngle == 0) {
            point.x = centerX;
            point.y = centerY - radius;
        } else if (sweepAngle >= 0 && sweepAngle < 90) {
            double tan = Math.tan(Math.toRadians(sweepAngle));
            //底数
            double radix = 1 + Math.pow(tan, 2);
            float height = (float) Math.sqrt(Math.pow(radius, 2) / radix);
            float width = (float) (height * tan);
            point.x = centerX + width;
            point.y = centerY - height;
        } else if (sweepAngle == 90) {
            point.x = centerX + radius;
            point.y = centerY;
        } else if (sweepAngle > 90 && sweepAngle <= 180) {
            double tan = Math.tan(Math.toRadians(180 - sweepAngle));
            //底数
            double radix = 1 + Math.pow(tan, 2);
            float height = (float) Math.sqrt(Math.pow(radius, 2) / radix);
            float width = (float) (height * tan);
            point.x = centerX + width;
            point.y = centerY + height;
        } else if (sweepAngle > 180 && sweepAngle < 270) {
            double tan = Math.tan(Math.toRadians(sweepAngle - 180));
            //底数
            double radix = 1 + Math.pow(tan, 2);
            float height = (float) Math.sqrt(Math.pow(radius, 2) / radix);
            float width = (float) (height * tan);
            point.x = centerX - width;
            point.y = centerY + height;
        } else if (sweepAngle == 270) {
            point.x = centerX - radius;
            point.y = centerY;
        } else if (sweepAngle > 270 && sweepAngle < 360) {
            double tan = Math.tan(Math.toRadians(360 - sweepAngle));
            //底数
            double radix = 1 + Math.pow(tan, 2);
            float height = (float) Math.sqrt(Math.pow(radius, 2) / radix);
            float width = (float) (height * tan);
            point.x = centerX - width;
            point.y = centerY - height;
        } else {
            point.x = centerX;
            point.y = centerY - radius;
        }
        return point;
    }

    public static Point getPointEndOfArcOnCircle(Point pointStart, int centerX, int centerY, int radius, double sweepAngle) {
        Point point = new Point();
        double angleToY = MathUtil.getAngleToY(pointStart.x, pointStart.y, centerX, centerY);
        sweepAngle = sweepAngle + angleToY;
        sweepAngle = sweepAngle % 360;
        point.x = (int) (centerX + radius * Math.sin(Math.toRadians(sweepAngle)));
        point.y = (int) (centerY - radius * Math.cos(Math.toRadians(sweepAngle)));
        return point;
    }


    public static float getAngleToY(float x, float y, int centerX, int centerY) {
        //起始点与Y轴的夹角,从Y轴上半轴起始算
        float angleToY = 0;
        int quadrant = getQuadrant(x, y, centerX, centerY);
        double sin = (x - centerX) / Math.hypot(x - centerX, y - centerY);
        int degree = (int) Math.round(Math.abs(Math.toDegrees(Math.asin(sin))));
        if (quadrant == 1) {
            angleToY = degree;
        } else if (quadrant == 2) {
            angleToY = 360 - degree;
        } else if (quadrant == 3) {
            angleToY = 180 + degree;
        } else if (quadrant == 4) {
            angleToY = 180 - degree;
        }
//        Log.i(TAG, "degree=" + degree + ",angle=" + angleToY + ",x=" + x + ",y=" + y);
        return angleToY;
    }

    /**
     * 坐标点相对于圆心所在象限
     *
     * @param x
     * @param y
     * @param centerX
     * @param centerY
     * @return
     */
    public static int getQuadrant(float x, float y, int centerX, int centerY) {
        float xTemp = x - centerX;
        float yTemp = y - centerY;
        if (xTemp >= 0) {
            return yTemp >= 0 ? 4 : 1;
        } else {
            return yTemp >= 0 ? 3 : 2;
        }

    }

    public static String ticket(String type) {
        StringBuffer ticket = new StringBuffer();
        Random random = new Random();
        if ("七星彩".equals(type)) {
            for (int index = 1; index <= 7; index++) {
                ticket.append(random.nextInt(9));
            }
        }

        return ticket.toString();
    }
}
